package bTree.LeetCode;

import java.util.HashSet;

public class 最长连续序列哈希 {
    int path = 1;
    int max = 0;

    public int longestConsecutive(int[] nums) {
        HashSet<Integer> hashSet = new HashSet<>();
        for (int num : nums) {
            hashSet.add(num);
        }
        for (int num : nums) {
            //是起点
            if (!hashSet.contains(num-1)) {
                int n = num;
                while (hashSet.contains(n + 1)) {
                    path++;
                    n += 1;
                }
                if(path>max){
                    max=path;
                }
                path=1;
            }
        }
        return max;
    }
}
